package Util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {

    public static void main(String[] args) {

        //FOI UTILIZADA O JSON MESSAGE
        String strJson = "{\"notificationType\":\"Bounce\",\"bounce\":{\"bounceSubType\":\"General\",\"bounceType\":\"Permanent\",\"reportingMTA\":\"dsn; aws-ses-mta-svc-iad-1e-large-i-3c84f950.us-east-1.amazon.com\",\"bouncedRecipients\":[{\"status\":\"5.1.1\",\"diagnosticCode\":\"smtp; 550 5.1.1 user unknown\",\"emailAddress\":\"bounce@simulator.amazonses.com\",\"action\":\"failed\"}],\"timestamp\":\"2013-07-17T02:13:01.000Z\",\"feedbackId\":\"0000013fea66ee11-6833803f-ee86-11e2-817c-dda88a178e6b-000000\"},\"mail\":{\"timestamp\":\"2013-07-17T02:13:00.000Z\",\"source\":\"bsmrio@gmail.com\",\"messageId\":\"0000013fea66eb70-34563277-6154-4149-8d1d-0f009e75db49-000000\",\"destination\":[\"bounce@simulator.amazonses.com\"]}}";
        //String strJson = "{\"notificationType\":\"Complaint\",\"complaint\":{\"userAgent\":\"Amazon SES Mailbox Simulator\",\"complainedRecipients\":[{\"emailAddress\":\"complaint@simulator.amazonses.com\"}],\"complaintFeedbackType\":\"abuse\",\"timestamp\":\"2013-07-17T03:10:26.000Z\",\"feedbackId\":\"0000013fea9b7e6c-6d7ab090-ee8e-11e2-8151-85b9ca06e863-000000\"},\"mail\":{\"timestamp\":\"2013-07-17T03:10:25.000Z\",\"source\":\"bsmrio@gmail.com\",\"messageId\":\"0000013fea9b7b67-2851c012-b48b-4802-b840-a3222fdc54cc-000000\",\"destination\":[\"complaint@simulator.amazonses.com\",\"suppressionlist@simulator.amazonses.com\"]}}";
       
        try {
            //instancia um novo JSONObject passando a string como entrada

            JSONObject jsonObj = new JSONObject(strJson);

            String strBounce = jsonObj.getString("bounce");
            String strEmail = jsonObj.getString("mail");

            System.out.println("Notification Type: " + jsonObj.getString("notificationType"));
            System.out.println("Bounce: " + jsonObj.getString("bounce"));
            System.out.println("Email: " + jsonObj.getString("mail"));
            
            //JSON INTERNA BOUNCE
            JSONObject objBounce = new JSONObject(strBounce);
            System.out.println("OBJETOS CONTIDOS DENTRO DA JSON INTERNA BOUNCE");
            System.out.println("    Timestamp: " + objBounce.get("timestamp"));
            System.out.println("    ReportingMTA: " + objBounce.get("reportingMTA"));
            System.out.println("    BounceSubType: " + objBounce.get("bounceSubType"));
            System.out.println("    FeedbackId: " + objBounce.get("feedbackId"));
            System.out.println("    BounceType: " + objBounce.get("bounceType"));

            //ARRAY CONTIDO DENTRO DO JSON INTERNO BOUNCE
            JSONArray bounceDetalhes = objBounce.getJSONArray("bouncedRecipients");
            System.out.println("    OBJETOS CONTIDOS DENTRO DO ARRAY DO JSON INTERNO BOUNCE RECIPIENTS");
            for (int i = 0; i < bounceDetalhes.length(); i++) {

                String strDetalhe = bounceDetalhes.get(i).toString();

                JSONObject jsonDetalhe = new JSONObject(strDetalhe);

                System.out.println("        DiagnosticCode: " + jsonDetalhe.get("diagnosticCode"));
                System.out.println("        Status: " + jsonDetalhe.get("status"));
                System.out.println("        Action: " + jsonDetalhe.get("action"));
                System.out.println("        EmailAddress: " + jsonDetalhe.get("emailAddress"));
               
            }
            
            //JSON INTERNA MAIL
            JSONObject objEmail = new JSONObject(strEmail);
            System.out.println("OBJETOS CONTIDOS DENTRO DA JSON INTERNA MAIL");
            System.out.println("    Timestamp: " + objEmail.getString("timestamp"));
            System.out.println("    Source: " + objEmail.getString("source"));
            System.out.println("    MessageId: " + objEmail.getString("messageId"));

            //ARRAY CONTIDO DENTRO DO JSON INTERNO EMAIL
            JSONArray destinatarios = objEmail.getJSONArray("destination");
            System.out.println("    OBJETOS CONTIDOS DENTRO DO ARRAY DO JSON INTERNO BOUNCE RECIPIENTS");
            for (int i = 0; i < destinatarios.length(); i++) {
                System.out.println("        Destinatário: " + destinatarios.get(i).toString());
            }

        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }
}
